﻿using System;
using FluentNHibernate.Testing;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using SevenDigital.EndUserManagement.Core;
using SevenDigital.EndUserManagement.Core.Repository;

namespace SevenDigital.EndUserManagement.Tests
{
	[TestFixture]
	public class UserFixture : BaseFixture
	{
		[Test]
		public void verify_mapping_correct() {
			var session = GetSession();

			new PersistenceSpecification<User>(session)
				.CheckProperty(x => x.Email, "testgmail.com")
				.CheckProperty(x => x.Password, "123")
				.CheckProperty(x => x.SecurityToken,
				               new SecurityToken(new DateTime(2001, 10, 1), "HelloWorld"))
				.VerifyTheMappings();
		}

		[Test]
		public void resetPassword_method_should_set_the_securityToken() {
			var repository  = new UserRepository(GetSessionFactory());

			var user = repository.MakePersistent(CreateTestUser());
			int userId = user.Id;

			Assert.That(user.SecurityToken, Is.Null);
			user.ResetPassword();
			repository.MakePersistent(user);
		

			var fromDB = repository.FindById(userId);
			Assert.That(fromDB.SecurityToken, Is.Not.Null);
		}

		private User CreateTestUser() {
			return new User {Email = "test2@7digital.com", Password = "helloworld"};
		}
	}
}